package com.amuse.amuse.service.impl;

import com.amuse.amuse.entity.PageBean;
import com.amuse.amuse.entity.facilities;
import com.amuse.amuse.entity.facilityReservations;
import com.amuse.amuse.entity.log;
import com.amuse.amuse.mapper.StudentsInfoDao;
import com.amuse.amuse.service.FacilityPermissionService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
@Service
public class FacilityPermissionServiceImpl implements FacilityPermissionService {
    @Autowired
    private com.amuse.amuse.mapper.facility_permissions.FacilityPermissionDao facilityPermissionDao;

    @Autowired
    private StudentsInfoDao studentsInfoDao;


    //    // 获取日志记录器
    private static final Logger logger = LoggerFactory.getLogger(log.class);

    /*
    * 管理员添加场地
    * */
    @Override
    public Integer addFacility(facilities facilities) {
        if (facilities != null){
            return facilityPermissionDao.addFacility(facilities);
        }else {
            logger.error("添加的场地信息不能为空");
            return null;
        }
    }

    /*
    * 管理员删除场地
    * */
    @Override
    public Integer deleteFacility(Integer facilityId) {
        if (facilityId != null){
            return facilityPermissionDao.deleteFacility(facilityId);}else {
            logger.error("删除的场地id不能为空");
            return null;
        }
    }

    /*
    * 更新场地信息
    * */
    @Override
    public Integer updateFacility(facilities facilities) {
        if (facilities != null){
            return facilityPermissionDao.updateFacility(facilities);
        }else {
            logger.error("更新的场地信息不能为空");
            return null;
        }

    }

    /*
    * 根据名称查询各个场地信息
    * */

    @Override
    public PageBean<facilities> findFacilityByTypeName(Integer pageNum, Integer pageSize, String typeName) {
        PageBean<facilities> facilityPageBean = new PageBean<>();
        PageHelper.startPage(1, 10);
        List<facilities> facilityByTypeName = facilityPermissionDao.findFacilityByTypeName(typeName);
        Page<facilities> page = (Page<facilities>)facilityByTypeName;
        facilityPageBean.setItems(page.getResult());
        facilityPageBean.setTotal(page.getTotal());
        return facilityPageBean;
    }


    /*
    * 学生预约场地
    * */
    @Override
    public Integer addFacilityReservations(Integer facilityId, Integer studentId, String startTime, String endTime, String reservationDate, String status) {
        if (facilityId == null || studentId == null || startTime == null || endTime == null || reservationDate == null || status == null){
            logger.error("预约信息不能为空");
            return null;
        }
        //判断输入的学号是否为六位，其能否在学生信息中找到该学号
        if (studentId.toString().length() != 6){
            logger.error("输入的学号长度不为六位");
            return null;
        }
        if (studentsInfoDao.findStudentsInfo(studentId) == null){
            logger.error("输入的学号不存在");
            return null;
        }else if (facilityPermissionDao.findFacilityReservations(studentId).size() == 0){
            return facilityPermissionDao.addFacilityReservations(facilityId,studentId,startTime,endTime,reservationDate,status);
        }else {
            logger.error("每人只能预约一次");
            return null;
        }

    }
    /*
    * 删除预约，撤销预约记录
    * */
    @Override
    public Integer deleteFacilityReservations(Integer studentId, Integer facilityId) {
        if (studentId != null && facilityId != null){
            return facilityPermissionDao.deleteFacilityReservations(studentId,facilityId);
        }else {
            logger.error("删除的学号或场地idchu");
            return null;
        }
    }
    /*
    * 查看预约信息
    * */

    @Override
    public PageBean<facilityReservations> findFacilityReservations(Integer pageNum, Integer pageSize, Integer studentId) {
        PageBean<facilityReservations> facilitiesBean = new PageBean<>();
        PageHelper.startPage(1, 10);
        List<facilityReservations> facilityReservations = facilityPermissionDao.findFacilityReservations(studentId);
        Page<facilityReservations> page = (Page<facilityReservations>)facilityReservations;
        facilitiesBean.setItems(page.getResult());
        facilitiesBean.setTotal(page.getTotal());
        return facilitiesBean;
    }

    /*
    * 学生修改预约信息
    * */

    @Override
    public Integer updateFacilityReservations(Integer studentId, Integer facilityId, String startTime, String endTime, String reservationDate) {
        if (studentId != null && facilityId != null && startTime != null && endTime != null && reservationDate != null){
            return facilityPermissionDao.updateFacilityReservations(studentId,facilityId,startTime,endTime,reservationDate);
        }else {
            logger.error("修改的学号或场地id不能为空");
            return null;
        }
    }
//    /*
//    * 实时更新场地状态
//    * */
//    @Override
//    public Integer updateFacilityStatus(Integer facilityId, String status) {
//        if (facilityId != null && status != null){
//            return facilityPermissionDao.updateFacilityAvailablity(facilityId,status);
//        }else {
//            logger.error("修改的场地id或状态不能为空");
//            return null;
//        }
//    }
}
